<?php

	include("dbconnect.php");
	require_once("ekgames.php");
	
	ob_end_clean(); //Clear all choed & printed item	
	
	//Receive then respond the request
	$requestFunction = receiveRequest("action");

	switch ($requestFunction) {
		case "loadallcard":
			echo loadAllCard();
			break;
		case "loadusercard":
			echo loadUserCard();
			break;
		case "loaddeck":
			echo loadAllCardOfDeck();
			break;
		case "countdeck":
			echo countUserDeck();
			break;
		case "loadlevelcard":
			echo loadLevelCard();
			break;
		case "editusercard":
			echo editUserCard();
			break;
		case "removeusercard":
			echo removeUserCard();
			break;
		
		default:
			echo "ERR#01: Bad request!";
	}
	
	mysql_close($con);
	
	//=======================================================================================
	
	//Load all user's card
	function loadAllCard () {
		$userid = receiveRequest("userid");
		
		if($userid == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT * FROM ucards WHERE USER_ID='$userid'";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		while($r = mysql_fetch_array($q)){
			$xml .= "<card>";
			$xml .= "<NUM>".$r['NUM']."</NUM>";  
			$xml .= "<USER_ID>".$r['USER_ID']."</USER_ID>";
			$xml .= "<CARD_ID>".$r['CARD_ID']."</CARD_ID>";  
			$xml .= "<TYPE>".$r['TYPE']."</TYPE>";    
			$xml .= "<RARITY>".$r['RARITY']."</RARITY>";
			$xml .= "<CARD_NAME>".$r['CARD_NAME']."</CARD_NAME>";
			$xml .= "<ATT>".$r['ATT']."</ATT>";  
			$xml .= "<ATT_RANGE>".$r['ATT_RANGE']."</ATT_RANGE>";
			$xml .= "<PIVOT>".$r['PIVOT']."</PIVOT>";  
			$xml .= "<COMBAT>".$r['COMBAT']."</COMBAT>";    
			$xml .= "<ELEMENT>".$r['ELEMENT']."</ELEMENT>";
			$xml .= "<HP>".$r['HP']."</HP>";
			$xml .= "<COST>".$r['COST']."</COST>";    
			$xml .= "<EXP>".$r['EXP']."</EXP>";
			$xml .= "<LEVEL>".$r['LEVEL']."</LEVEL>";
			$xml .= "<DESCRIPTION>".$r['DESCRIPTION']."</DESCRIPTION>";    
			$xml .= "<IMG>".$r['IMG']."</IMG>";
			$xml .= "<MODEL>".$r['MODEL']."</MODEL>";
			$xml .= "</card>";
		}
		$xml .= "</root>";
		
		return $xml;
	}
	
	//Load user's card by card id
	function loadUserCard () {
		$userid = receiveRequest("userid");
		$cardid = receiveRequest("cardid");
		
		if($userid == null or $cardid == null) {
			badRequest();
			return;
		}
			
		$sql = "SELECT * FROM ucards WHERE USER_ID='$userid' and NUM=$cardid";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		while($r = mysql_fetch_array($q)){
			$xml .= "<card>";
			$xml .= "<NUM>".$r['NUM']."</NUM>";  
			$xml .= "<USER_ID>".$r['USER_ID']."</USER_ID>";
			$xml .= "<CARD_ID>".$r['CARD_ID']."</CARD_ID>";  
			$xml .= "<TYPE>".$r['TYPE']."</TYPE>";    
			$xml .= "<RARITY>".$r['RARITY']."</RARITY>";
			$xml .= "<CARD_NAME>".$r['CARD_NAME']."</CARD_NAME>";
			$xml .= "<ATT>".$r['ATT']."</ATT>";  
			$xml .= "<ATT_RANGE>".$r['ATT_RANGE']."</ATT_RANGE>";
			$xml .= "<PIVOT>".$r['PIVOT']."</PIVOT>";  
			$xml .= "<COMBAT>".$r['COMBAT']."</COMBAT>";    
			$xml .= "<ELEMENT>".$r['ELEMENT']."</ELEMENT>";
			$xml .= "<HP>".$r['HP']."</HP>";
			$xml .= "<COST>".$r['COST']."</COST>";    
			$xml .= "<EXP>".$r['EXP']."</EXP>";
			$xml .= "<LEVEL>".$r['LEVEL']."</LEVEL>";
			$xml .= "<DESCRIPTION>".$r['DESCRIPTION']."</DESCRIPTION>";    
			$xml .= "<IMG>".$r['IMG']."</IMG>";
			$xml .= "<MODEL>".$r['MODEL']."</MODEL>";
			$xml .= "</card>";
		}
		$xml .= "</root>";
		
		return $xml;
	}
	
	//Load all card of user deck by deck id
	function loadAllCardOfDeck () {
		$userid = receiveRequest("userid");
		$deckid = receiveRequest("deckid");
		
		if($userid == null or $deckid == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT * FROM udecks LEFT JOIN ucards ON udecks.CARD_ID = ucards.NUM where udecks.DECK_ID=$deckid and udecks.USER_ID='$userid'";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		while($r = mysql_fetch_array($q)){
			$xml .= "<card>";
			$xml .= "<NUM>".$r['NUM']."</NUM>";  
			$xml .= "<USER_ID>".$r['USER_ID']."</USER_ID>";
			$xml .= "<CARD_ID>".$r['CARD_ID']."</CARD_ID>";  
			$xml .= "<TYPE>".$r['TYPE']."</TYPE>";    
			$xml .= "<RARITY>".$r['RARITY']."</RARITY>";
			$xml .= "<CARD_NAME>".$r['CARD_NAME']."</CARD_NAME>";
			$xml .= "<ATT>".$r['ATT']."</ATT>";  
			$xml .= "<ATT_RANGE>".$r['ATT_RANGE']."</ATT_RANGE>";
			$xml .= "<PIVOT>".$r['PIVOT']."</PIVOT>";  
			$xml .= "<COMBAT>".$r['COMBAT']."</COMBAT>";    
			$xml .= "<ELEMENT>".$r['ELEMENT']."</ELEMENT>";
			$xml .= "<HP>".$r['HP']."</HP>";
			$xml .= "<COST>".$r['COST']."</COST>";    
			$xml .= "<EXP>".$r['EXP']."</EXP>";
			$xml .= "<LEVEL>".$r['LEVEL']."</LEVEL>";
			$xml .= "<DESCRIPTION>".$r['DESCRIPTION']."</DESCRIPTION>";    
			$xml .= "<IMG>".$r['IMG']."</IMG>";
			$xml .= "<MODEL>".$r['MODEL']."</MODEL>";
			$xml .= "</card>";
		}
		$xml .= "</root>";
		
		return $xml;
	}
	
	//Count number of user's decks
	function countUserDeck () {
		$userid = receiveRequest("userid");
		
		if($userid == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT DISTINCT DECK_ID FROM udecks WHERE USER_ID='$userid'";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		$count = mysql_num_rows($q);
			
		$xml = "<root>";
		$xml .= "<deck>";
		$xml .= "<count>$count</count>";  
		$xml .= "</deck>";
		$xml .= "</root>";
		
		return $xml;
	}
	
	//Load level card from system
	function loadLevelCard () {
		$cardid = receiveRequest("cardid");
		
		if($cardid == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT * FROM level_cards WHERE ID=$cardid";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		while($r = mysql_fetch_array($q)){
			$xml .= "<levelcard>";
			$xml .= "<ID>".$r['ID']."</ID>";  
			$xml .= "<MAX_LEVEL>".$r['MAX_LEVEL']."</MAX_LEVEL>";
			$xml .= "<ATT_LV1>".$r['ATT_LV1']."</ATT_LV1>";  
			$xml .= "<ATT_LV2>".$r['ATT_LV2']."</ATT_LV2>";    
			$xml .= "<ATT_LV3>".$r['ATT_LV3']."</ATT_LV3>";
			$xml .= "<ATT_LV4>".$r['ATT_LV4']."</ATT_LV4>";
			$xml .= "<ATT_LV5>".$r['ATT_LV5']."</ATT_LV5>";  
			$xml .= "<HP_LV1>".$r['HP_LV1']."</HP_LV1>";
			$xml .= "<HP_LV2>".$r['HP_LV2']."</HP_LV2>";  
			$xml .= "<HP_LV3>".$r['HP_LV3']."</HP_LV3>";    
			$xml .= "<HP_LV4>".$r['HP_LV4']."</HP_LV4>";
			$xml .= "<HP_LV5>".$r['HP_LV5']."</HP_LV5>";
			$xml .= "<MANA_LV1>".$r['MANA_LV1']."</MANA_LV1>";    
			$xml .= "<MANA_LV2>".$r['MANA_LV2']."</MANA_LV2>";
			$xml .= "<MANA_LV3>".$r['MANA_LV3']."</MANA_LV3>";
			$xml .= "<MANA_LV4>".$r['MANA_LV4']."</MANA_LV4>";    
			$xml .= "<MANA_LV5>".$r['MANA_LV5']."</MANA_LV5>";
			$xml .= "<IMG_LV1>".$r['IMG_LV1']."</IMG_LV1>";    
			$xml .= "<IMG_LV2>".$r['IMG_LV2']."</IMG_LV2>";
			$xml .= "<IMG_LV3>".$r['IMG_LV3']."</IMG_LV3>";
			$xml .= "<IMG_LV4>".$r['IMG_LV4']."</IMG_LV4>";    
			$xml .= "<IMG_LV5>".$r['IMG_LV5']."</IMG_LV5>";
			$xml .= "</levelcard>";
		}
		$xml .= "</root>";
		
		return $xml;
	}
	
	//Edit user's cards
	function editUserCard () {
		$uid = receiveRequest("uid");
		$cid = receiveRequest("cid");
			
		$att = receiveRequest("att");
		$hp = receiveRequest("hp");
		$exp= receiveRequest("exp");
		$cost = receiveRequest("cost");
		$lv = receiveRequest("level");
		$img = receiveRequest("img");
		
		if($uid == null or $cid == null) {
			badRequest();
			return;
		}
		
		$sql = "UPDATE ucards SET ATT='$att', HP='$hp', EXP='$exp', LEVEL='$lv', COST='$cost', IMG='$img' WHERE USER_ID='$uid' AND NUM='$cid'";
		mysql_query($sql);
	}
	
	//Remove user's card by card id
	function removeUserCard () {

		$userid = receiveRequest("userid");
		$cardid = receiveRequest("cardid");

		if($userid == null or $cardid == null)
		{
			badRequest();
			return;
		}
		
		//Remove from cards collection
		$sql = "DELETE FROM ucards WHERE USER_ID='$userid' AND NUM='$cardid'";
		mysql_query($sql);
			
		//Remove from deck
		$sql = "DELETE FROM udecks WHERE USER_ID='$userid' AND CARD_ID='$cardid'";
		mysql_query($sql);
		
		echo 1;
	}

?>
